﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Workflow.Activities;
using System.Workflow.Runtime;

using MediaXenterWPF.UserControl;
using NetExp.MediaXenter.StateWorkflow;
using NetExp.MediaXenter.Utilities;
using NetExp.MediaXenter.Database;

namespace MediaXenterWPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class frmPrincipale : Window
    {
        private SettingsDS _settingsDS = null;
        private KeyboardDS _keyboardDS = null;
        private KeyboardManager keyboardManager;
        private NavigationHelper _navigationHelper;
        private StyleHelper _styleHelper;
        ThemesResources _themesResources;

        PaginaPrincipale principale = null;
        PaginaFoto foto = null;
        PaginaImpostazioni impostazioni = null;
        PaginaMusica musica = null;
        PaginaPlugin plugin = null;
        PaginaTelevisione televisione = null;
        PaginaVideo video = null;

        public frmPrincipale()
        {
            InitializeComponent();

            //WorkflowRuntime workflowRuntime = new WorkflowRuntime();
            //WorkflowInstance workflowInstance = workflowRuntime.CreateWorkflow(typeof(MediaXenterSequential));
            //workflowInstance.Start();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _settingsDS = SettingsHelper.ReadSettings();
            _keyboardDS = KeyboardHelper.ReadKeyboardFile();
            _navigationHelper = new NavigationHelper();
            _themesResources = new ThemesResources();
            
            if (Helper.isFirstStartup())
            {
                Helper.Inizialize();
                SettingsHelper.DefaultSetting(_settingsDS);
                KeyboardHelper.DefaultKeyboardSetting(_keyboardDS);
            }
            _themesResources.LoadTheme(_settingsDS.General[0].Theme);
            _styleHelper = new StyleHelper(_settingsDS.General[0].Theme);
            keyboardManager = new KeyboardManager(_keyboardDS);
            principale = new PaginaPrincipale(this.ActualWidth, this.ActualHeight, _settingsDS, keyboardManager, _themesResources, _styleHelper);
            principale.OnPrincipaleAnimazioneEnd += new Delegates.OnPrincipaleAnimazioneEnd(principale_OnPrincipaleAnimazioneEnd);
            foto = new PaginaFoto(_settingsDS, keyboardManager, _themesResources);
            impostazioni = new PaginaImpostazioni(_settingsDS, keyboardManager, _themesResources, _styleHelper);
            impostazioni.OnImpostazioniAnimazioneEnd += new Delegates.OnImpostazioniAnimazioneEnd(impostazioni_OnImpostazioniAnimazioneEnd);
            musica = new PaginaMusica(_settingsDS, keyboardManager, _themesResources);
            plugin = new PaginaPlugin(_settingsDS, keyboardManager, _themesResources);
            televisione = new PaginaTelevisione(_settingsDS, keyboardManager, _themesResources);
            video = new PaginaVideo(_settingsDS, keyboardManager, _themesResources);
            areaContenitore.Children.Add(principale);
            _navigationHelper.SetPosition(PagePosition.Principale);

            imgPrincipale.ImageSource = _themesResources.Sfondo;
        }

        void impostazioni_OnImpostazioniAnimazioneEnd()
        {
            areaContenitore.Children.Remove(impostazioni);
            areaContenitore.Children.Add(principale);
            principale.ZoomAnimation();
        }

        void principale_OnPrincipaleAnimazioneEnd()
        {
            PagePosition position = principale.GetSelectedPage();
            areaContenitore.Children.Remove(principale);
            switch (position)
            {
                case PagePosition.Video:
                    areaContenitore.Children.Add(video);
                    break;
                case PagePosition.Foto:
                    areaContenitore.Children.Add(foto);
                    break;
                case PagePosition.Musica:
                    areaContenitore.Children.Add(musica);
                    break;
                case PagePosition.Impostazioni:
                    areaContenitore.Children.Add(impostazioni);
                    impostazioni.ZoomAnimation();
                    break;
                case PagePosition.Plugin:
                    areaContenitore.Children.Add(plugin);
                    break;
                case PagePosition.Televisione:
                    areaContenitore.Children.Add(televisione);
                    break;
                default:
                    break;
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == keyboardManager.Exit)
                ExitKey();
            else if (e.Key == keyboardManager.MoveUp || e.Key == keyboardManager.MoveDown)
                MoveUpMoveDownKey(e);
            else if (e.Key == keyboardManager.MoveLeft || e.Key == keyboardManager.MoveRight)
                MoveLeftMoveRightKey(e);
            else if (e.Key == keyboardManager.Selected)
                SelectedKey();
            else if (e.Key == keyboardManager.Back)
                BackKey();
        }

        private void btnCaricaTasti_Click(object sender, RoutedEventArgs e)
        {
            KeyboardHelper.ReadKeyboardFile();
        }

        private void MoveLeftMoveRightKey(KeyEventArgs e)
        {
            PagePosition posizione = _navigationHelper.GetPosition();
            switch (posizione)
            {
                case PagePosition.Principale:
                    break;
                case PagePosition.Video:
                    break;
                case PagePosition.Foto:
                    break;
                case PagePosition.Musica:
                    break;
                case PagePosition.Impostazioni:
                    ImpostazioniPosition position = impostazioni.GetImpostazioniSelectedPage();
                    _navigationHelper.SetImpostazioniPosition(position);
                    if (position != ImpostazioniPosition.Nessuno)
                    {
                        if (e.Key == keyboardManager.MoveRight)
                        {
                            impostazioni.ClearButtonSelection();
                            impostazioni.SetSubSelection(position);
                        }
                    }
                    else
                    {
                    }

                    break;
                case PagePosition.Plugin:
                    break;
                case PagePosition.Televisione:
                    break;
                default:
                    break;
            }
        }

        private void MoveUpMoveDownKey(KeyEventArgs e)
        {
            switch (_navigationHelper.GetPosition())
            {
                case PagePosition.Principale:
                    principale.AnimazionePulsanti(e.Key);
                    break;
                case PagePosition.Video:
                    break;
                case PagePosition.Foto:
                    break;
                case PagePosition.Musica:
                    break;
                case PagePosition.Impostazioni:
                    ImpostazioniPosition impostazioniPosition = _navigationHelper.GetImpostazioniPosition();
                    
                    if (impostazioniPosition == ImpostazioniPosition.Nessuno)
                        impostazioni.AnimazionePulsanti(e.Key);
                    else
                        impostazioni.AnimazioneSubPulsanti(impostazioniPosition, e.Key);

                    break;
                case PagePosition.Plugin:
                    break;
                case PagePosition.Televisione:
                    break;
            }
        }

        private void SelectedKey()
        {
            switch (_navigationHelper.GetPosition())
            {
                case PagePosition.Principale:
                    PagePosition position = principale.GetSelectedPage();
                    principale.ReductionAnimation();
                    _navigationHelper.SetPosition(position);
                    break;
                case PagePosition.Video:
                    break;
                case PagePosition.Foto:
                    break;
                case PagePosition.Musica:
                    break;
                case PagePosition.Impostazioni:
                    break;
                case PagePosition.Televisione:
                    break;
                default:
                    break;
            }

        }

        private void ExitKey()
        {
            this.Close();
        }
        private void BackKey()
        {
            switch (_navigationHelper.GetPosition())
            {
                case PagePosition.Principale:
                    PagePosition position = principale.GetSelectedPage();
                    principale.ReductionAnimation();
                    _navigationHelper.SetPosition(position);
                    break;
                case PagePosition.Video:
                    break;
                case PagePosition.Foto:
                    break;
                case PagePosition.Musica:
                    break;
                case PagePosition.Impostazioni:
                    impostazioni.ReductionAnimation();
                    _navigationHelper.SetPosition(PagePosition.Principale);
                    break;
                case PagePosition.Televisione:
                    break;
                default:
                    break;
            }
        }
    }
}
